<?php
    namespace app\admin\model;
    use think\Exception;
    use think\facade\Db;

    class Order extends Common {
        //自定义内容
        public function getStatusTextAttr($value,$data)
        {
            $status = [-1=>'审核未通过',1=>'待审核',2=>'已通过'];
            if(isset($status[(int)$data['status']])){
                return $status[(int)$data['status']];
            }else{
                return '';
            }
        }
        public function getAdminIdNameAttr($value,$data){
            if(!empty($data['admin_id'])){
                $admin_name = Admin::where('id',$data['admin_id'])->value('truename');
            }else{
                $admin_name = '';
            }
            return $admin_name;
        }
        public static function onBeforeUpdate($data)
        {
            unset($data['create_time']);
            unset($data['update_time']);
            return $data;
        }
        public static function onAfterInsert($data)
        {
            //查询所有业务主管uid
            $ids = Access::where('roles_id',6)->column('admin_id');
            if(!empty($ids)){
                foreach ($ids as $value){
                    $message_data = [
                        'uid'=>$value,  //业务主管
                        'event_type'=>'send_message',
                        'type'=>'client_message',
                        'message'=>['v'=>'relist'],
                    ];
                    event('send_message',$message_data);
                }
            }

        }
        public static function onAfterUpdate($data)
        {
            $data=db::name('order')->find($data['id']);
            if($data['status'] == 1){
                //查询所有业务主管uid
                $ids = Access::where('roles_id',6)->column('admin_id');
                if(!empty($ids)){
                    foreach ($ids as $value){
                        $message_data = [
                            'uid'=>$value,  //业务主管
                            'event_type'=>'send_message',
                            'type'=>'client_message',
                            'message'=>['v'=>'relist'],
                        ];
                        event('send_message',$message_data);
                    }
                }
            }else{
                $message_data = [
                    'uid'=>$data['admin_id'],
                    'event_type'=>'send_message',
                    'type'=>'client_message',
                    'message'=>['v'=>'relist'],
                ];
                event('send_message',$message_data);
            }
            if($data['status'] == 2){
                $company_col=json_decode($data['company']);
                $update['id']=$data['company_id'];
                foreach($company_col as $k=>$v){
                    if($v){
                        $update[$k] = $k=='area'?json_encode($v):$v;
                    }
                    
                }
                Company::update($update);
            }

        }
        public function orderCompany(){
            return $this->belongsTo(Company::class)->append(['thumbFile']);
        }
        public function setCompanyAttr($value,$data){
            if($value){
                $value=json_encode($value);
            }
            return $value;
        }
        public function getCompanyAttr($value,$data){
            if($value){
                $value=json_decode($value,true);
                if($value !== null) {
                    $value['thumbFile'] = '';
                    if (!empty($value['thumb'])) {
                        $value['thumbFile'] = request()->root(true) . db::name('files')->where('id', $value['thumb'])->value('filepath');
                    }
                    $value['areaName'] = '';
                    if (!empty($value['area'])) {
                        $name = Classify::Where('id', 'in', $value['area'])->column('title');
                        $value['areaName'] = implode('', $name);
                    }
                }
            }
            return $value;
        }

    }
?>